//
//  DatabaseSql.m
//  微他
//
//  Created by 微他 on 14-8-5.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "DatabaseSql.h"

@implementation DatabaseSql

//执行sql
+ (void)excute:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instance];
    if (sqlString != nil) {
        [dbhelper executeByQueue:sqlString];
    }
}
//执行sql查询
+ (NSArray *)query:(NSString *)sqlString
{
    DbHelper *dbhelper = [DbHelper instance];
    if (sqlString == nil) {
        return nil;
    }
    return [dbhelper queryDbToDictionaryArray:nil sql:sqlString];
}
//转义'
+ (NSString *)esc:(NSString *)string
{
    NSMutableString *escString = [NSMutableString stringWithFormat:@"%@",string];
    for (int i=0; i<escString.length; i++) {
//        NSString *ch=[escString characterAtIndex:i];
        NSString *ch = [escString substringWithRange:NSMakeRange(i, 1)];
        if ([ch isEqualToString: @"\'"]) {
            [escString replaceCharactersInRange:NSMakeRange(i, 1) withString:@"''"];
            i++;
        }
    }
    return escString;
}

+ (NSArray *)queryAllMember
{
    NSString *sqlString = @"select * from user_member order by logintime desc";
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)queryProvince
{
    NSString *sqlString = @"select id,proname as name from user_province where cid = 1";
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)queryCityid:(NSString *)provid
{
    
    
    NSString *sqlString = [NSString stringWithFormat:@"select id,cityname as name,provid from user_city where provid = %@",provid];
    
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)findprovid:(NSString *)provid
                 cityID:(NSString *)cityId
{
    NSString *sqlString = [NSString stringWithFormat:@"select  id,distname as name, cityid,provid from user_district where provid = %@ and cityid = %@",provid,cityId];
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)findprovinceName:(NSString *)provid
{
    NSString *sqlString = [NSString stringWithFormat:@"select proname as name from user_province where id = %@",provid];
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)findcityName:(NSString *)cityid
{
    NSString *sqlString = [NSString stringWithFormat:@"select cityname as name from user_city where id = %@",cityid];
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)finddistrictName:(NSString *)districtid
{
    NSString *sqlString = [NSString stringWithFormat:@"select distname as name from user_district where id = %@",districtid];
    return [DatabaseSql query:sqlString];
}

+ (NSArray *)queryAllProvinces {
    NSString *sql=[NSString stringWithFormat:@"select proname,shortname,id from user_province where cid=1"];
    return [DatabaseSql query:sql];
}

+ (NSArray *)queryCityForProvinces:(NSString *)provinceId {
    NSString *regionsql=[NSString stringWithFormat:@"select cityname as name,id from user_city where provid='%d'",[provinceId intValue]];
    NSArray *fromids=[DatabaseSql query:regionsql];
    
    return fromids;
}

+ (NSArray *)queryDistrict:(NSString *)Id {
    NSString *sql=[NSString stringWithFormat:@"select distname from user_district  where provid='%d'",[Id intValue]];
    return [DatabaseSql query:sql];
}

+ (NSString *)findproidbasename:(NSString *)proname{
    NSString *sql=[NSString stringWithFormat:@"select id from user_province  where proname='%@'",proname];
    NSArray *array=[DatabaseSql query:sql];
    if (array) {
        return array[0][@"id"];
    }
    return @"";
}

+ (NSString *)findcityidbasename:(NSString *)cityname{
    NSString *sql=[NSString stringWithFormat:@"select id from user_city  where cityname='%@'",cityname];
    NSArray *array=[DatabaseSql query:sql];
    if (array) {
        return array[0][@"id"];
    }
    return @"";
}

//插入pushkey
+ (NSArray *)insertPushKey:(NSString *)pushkey
{
    NSString * sqlString = [NSString stringWithFormat:@"insert into user(pushkey) values(?)"];
    return [DatabaseSql query:sqlString];
}
@end
